package cn.tedu.sentinel01.controller;


import cn.tedu.sentinel01.service.HelloService;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String sayhi(String name){
        System.out.println(name);
        //准备 初始化 资源
        Entry entry = null;
        //指定资源 判断规则
        try {
            entry = SphU.entry("sayhi");
            //处理结果

            String result = helloService.hello(name);
            return result;
        } catch (BlockException e) {
           log.info("当前资源收到限制了");
        }finally {
            //释放资源
            if(entry!=null){
                entry.exit();
            }
        }
       return "当前资源收到限制了";
    }
}
